﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ProjectSem40701.Admin
{
    public partial class ExamEdit : System.Web.UI.Page
    {
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam = new 
                    AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();
                DataTable dttblExam = 
                    dpExam.Get_tbl_Exam_Select_All_By_ExamID(Convert.ToInt32(Request.QueryString["exid"].ToString()));
                this.txtExamName.Text = dttblExam.Rows[0]["ExamName"].ToString();
                this.txtDescription.Value = Server.HtmlDecode(dttblExam.Rows[0]["ExamDescription"].ToString());
                //this.txtStartTime.Text = dttblExam.Rows[0]["ExamStartTime"].ToString();
                //this.txtEndTime.Text = dttblExam.Rows[0]["ExamEndTime"].ToString();

                this.txtStartDate.Text = Convert.ToDateTime(dttblExam.Rows[0]["ExamStartTime"].ToString()).ToString("MM/dd/yyyy");
                this.txtStartTime.Text = Convert.ToDateTime(dttblExam.Rows[0]["ExamStartTime"].ToString()).ToString("hh:mm:ss tt");

                this.txtEndDate.Text = Convert.ToDateTime(dttblExam.Rows[0]["ExamEndTime"].ToString()).ToString("MM/dd/yyyy");
                this.txtEndTime.Text = Convert.ToDateTime(dttblExam.Rows[0]["ExamEndTime"].ToString()).ToString("hh:mm:ss tt");

                foreach (ListItem item in cbbStatus.Items)
                {
                    if (item.Value == dttblExam.Rows[0]["ExamStatus"].ToString())
                    {
                        item.Selected = true;
                    }
                }
                checkTest.Checked = Convert.ToBoolean(dttblExam.Rows[0]["ExamIsCurrent"].ToString());
            }
            catch
            { 
                //show error here
            }
        }

        /// <summary>
        /// Cập nhật exam
        /// </summary>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam =
                    new AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();

                if (this.checkTest.Checked)
                {
                    ResetIsCurrent();
                }
                if (this.cbbStatus.SelectedValue.ToString() == "1")
                {
                    ResetStatus();
                }

                int ok = dpExam.tbl_Exam_Update_By_ExamID(
                    this.txtExamName.Text.Trim(), //Exam name
                    Server.HtmlEncode(this.txtDescription.Value.Trim()), //Description
                    Convert.ToDateTime(this.txtStartDate.Text.Trim() + " " + this.txtStartTime.Text.Trim()),  //Start date
                    Convert.ToInt32(this.cbbStatus.SelectedValue.ToString()), //status
                    this.checkTest.Checked, Convert.ToDateTime(this.txtEndDate.Text.Trim() + " " + this.txtEndTime.Text.Trim()), //end time
                    Convert.ToInt32(Request.QueryString["exid"].ToString())); //exam id
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                { 
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            { 
                //show error here
                Show_Dialog(FAIL);
            }
        }

        /// <summary>
        /// Reset tat ca exam status ve 0
        /// </summary>
        private void ResetStatus()
        {
            try
            {
                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam =
                    new AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();
                int ok = dpExam.tbl_Exam_Status_Reset(); 
                if (ok < 1)
                {
                    throw new Exception("Reset fail");
                }
            }
            catch
            {
                //bao that bai
                throw new Exception("Reset fail");
            }
        }

        /// <summary>
        /// Reset tat ca exam iscurrent ve 0
        /// </summary>
        private void ResetIsCurrent()
        {
            try
            {
                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam =
                    new AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();
                int ok = dpExam.tbl_Exam_IsCurrent_Reset();
                if (ok < 1)
                {
                    throw new Exception("Reset fail");
                }
            }
            catch
            {
                //bao that bai
                throw new Exception("Reset fail");
            }
        }

        protected void lblStatus_DataBinding(object sender, EventArgs e)
        {
            try
            {
                Label lblStatus = (Label)sender;
                lblStatus.Text = (lblStatus.Text.Trim() == "1" ? "Kích hoạt" : "Khóa");
            }
            catch
            { 
                //show error
            }
            
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter dpExam
                    = new AdminXSD.ExamTableAdapters.tbl_ExamTableAdapter();
                int ok = dpExam.tbl_Exam_Delete_By_ExamID(Convert.ToInt32(Request.QueryString["exid"].ToString()));
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog_Del(SUCCESS);
                }
                else
                { 
                    //bao that bai
                    Show_Dialog_Del(FAIL);
                }
            }
            catch
            { 
                //bao that bai
                Show_Dialog_Del(FAIL);
            }
        }

        protected void btnAddRound_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("RoundAdd.aspx?exid=" + Convert.ToInt32(Request.QueryString["exid"].ToString()));
            }
            catch
            { 
                //bao loi
            }
        }

        /// <summary>
        /// Đóng modal popup
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Exam.aspx");
                    break;
                case FAIL:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// show dialog
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Cập nhật thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Cập nhật thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, cập nhật thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnUpdate_ModalPopupExtender.Enabled = true;
            this.btnUpdate_ModalPopupExtender.Show();
        }

        /// <summary>
        /// show dialog neu an xoa
        /// </summary>
        private void Show_Dialog_Del(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Xóa thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Xóa thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, xóa thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnDelete_ModalPopupExtender.Enabled = true;
            this.btnDelete_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

    }
}
